<?php


namespace App\Libs\Sms;


class SmsService
{

    //发送短信
    public static function sendMessage($mobile,$msg,$sign='',$needstatus = true,$port='',$sendtime='',$mergerid='')
    {
        $mobile = is_array($mobile) ? $mobile : [(string)$mobile];
        $url = 'http://www.qybor.com:8500/shortMessage';
        $params = [
            'username'=>config('sms.qybor.username'),
            'passwd'=>config('sms.qybor.passwd'),
            'needstatus'=>$needstatus,
            'port'=>$port,
            'sendtime'=>$sendtime,
            'mergerid'=>$mergerid,
            'phone'=>implode(",", $mobile),
            'msg'=>$sign.$msg,
        ];
        info('企业宝发送请求参数', $params);
        $r =  get($url.'?'.\GuzzleHttp\Psr7\build_query($params))->getBody()->getContents();
        info('企业宝发送返回值', [$r]);
        return $r;
    }

    //余额查询
    public function surplus()
    {
        $url = 'http://www.qybor.com:8500/account/balance';
        $params = [
            'account'=>config('sms.qybor.username'),
            'signature'=>md5(config('sms.qybor.username').config('sms.qybor.passwd').time()),
            'timestamp'=>time(),
        ];
        info('企业宝查询余额参数', $params);
        $r =  get($url.'?'.\GuzzleHttp\Psr7\build_query($params))->getBody()->getContents();
        info('企业宝查询余额参数', [$r]);
        return data_get(json_decode($r), 'data.balance','-1');
    }

    //短信状态拉取
    public function smsReport($num = 30)
    {
        $url = 'http://www.qybor.com:5555/smsreport';
        $date = date('Y-m-d H:i:s');
        $params = [
            'username'=>config('sms.qybor.username'),
            'passwd'=>md5(config('sms.qybor.passwd').$date),
            'datetime'=>$date,
            'num'=>$num,
        ];
        info('企业宝查询余额参数', $params);
        $r =  get($url.'?'.\GuzzleHttp\Psr7\build_query($params))->getBody()->getContents();
        dd($r);
        info('企业宝查询余额参数', [$r]);
        return $r;
    }

}